package {
	import flash.text.TextFormat;	
	import flash.text.TextFieldAutoSize;	
	import flash.text.TextField;	
	import flash.display.Bitmap;
	
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.shaders.GouraudShader;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.papervision3d.view.layer.util.ViewportLayerSortMode;
	
	import com.kvc.extend.papervision3d.FourCamerasView;
	import com.kvc.extend.papervision3d.PlaneGrid3D;		

	/**
	 * @author kevincao
	 */
	[SWF(backgroundColor="0xCCCCCC", width="700", height="450", frameRate="60")]
	public class LogoDemo extends FourCamerasView {

		[Embed(source="../assets/logo/kvcLogo.dae", mimeType="application/octet-stream")]
		private var LogoAsset : Class;

		[Embed(source="../assets/logo/pattern.jpg")]
		private var TextureAsset : Class;

		private var logo : DAE;
		private var light : PointLight3D;

		public function LogoDemo(viewportWidth : Number = 640, viewportHeight : Number = 480, scaleToStage : Boolean = true, interactive : Boolean = false) {
			super(viewportWidth, viewportHeight, scaleToStage, interactive);
			init3D();
			createTips();
		}
		
		private function createTips() : void {
			var info : TextField = new TextField();
			info.autoSize = TextFieldAutoSize.LEFT;
			var tf : TextFormat = new TextFormat("Arial", 10);
			info.defaultTextFormat = tf;
			info.text = "Mouse :\nDrag = Oribt \nCtrl+Drag = Track \nAlt+Drag / Wheel = Dolly\n";
			info.appendText("Keyboard :\np = Perspective View \nt = Top View \nf = Front View \nl = Left View \nr = Reset");
			addChild(info);
		}

		private function init3D() : void {
			light = new PointLight3D(false);
			light.z = -10000;
			light.y = 40000;
			light.x = 30000;
			scene.addChild(light);
			
			var bmat : BitmapMaterial = new BitmapMaterial(Bitmap(new TextureAsset()).bitmapData);
			var shader : GouraudShader = new GouraudShader(light, 0xFFFFFF, 0x666666);
			var shadeMat : ShadedMaterial = new ShadedMaterial(bmat, shader);
			var mList : MaterialsList = new MaterialsList();
			mList.addMaterial(shadeMat, "kvcLogoSG");

			logo = new DAE();
			logo.addEventListener(FileLoadEvent.LOAD_COMPLETE, onLoad);
			logo.load(XML(new LogoAsset()), mList);
			
			scene.addChild(new PlaneGrid3D());
			
			viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
		}

		private function onLoad(evt : FileLoadEvent) : void {
			scene.addChild(logo);
			startRendering();
		}
	}
}
